#!/usr/bin/env ruby
#
#  Created by sjavey on 2007-07-10.
#  Copyright (c) 2007. All rights reserved.

#
# To run this program you must add /full-path.../note19/trunk to your RUBYLIB
# environment variable.
#

require 'n19gpx/document'

class ElevationChart
  
  attr_reader :elevation_time
  attr_reader :gpx
  
  def initialize(gpxfile)
    @gpx = N19GPX::Document.new(gpxfile)
    @index = -1
    @counter = -1
    @elevation_time = ""
  end
    
  def addPoint(point) 
    @elevation_time << "{ x: #{point.pst_hour_minute} , y: #{point.ele} },\n"
  end
  
  def trackHtml(track)
    track.points.each { |point|
      addPoint(point)
    }
  end

  def html
    @htmlHeader = <<EOS
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>note19 Scripts</title>
    
    #{elevationChart()}

  </head>
  <body>
  	<div id="chartTest1"></div>
  </body>
</html>
EOS
  end
  
  def tracksHtml
    @gpx.tracks.each {|track|
     trackHtml(track)
    }
  end
  
  def timeLabel
    min = @gpx.to_hour_min(@gpx.start_time)
    max = @gpx.to_hour_min(@gpx.end_time)
    i = min
    s = ""
    while (i < max) 
      s << sprintf("%.2f", i) << ","
      i = i + 0.5
    end
    return s << sprintf("%.2f",max)
  end
  
  def elevationChart
    tracksHtml
    str = <<EOS
<script>djConfig={isDebug:true}</script>
<script src="http://javey.net/scripts/dojo/js/dojo-0.4.1-ajax/dojo.js"></script>
<script>
      dojo.require("dojo.collections.Store");
      dojo.require("dojo.charting.Chart");
      dojo.require('dojo.json');
      var elevationJson = [
        #{@elevation_time[0..@elevation_time.length-3]}
      ]
      var elevationStore = new dojo.collections.Store();
      elevationStore.setData(elevationJson);
      var s1 = new dojo.charting.Series({
      				dataSource:elevationStore,
      				bindings:{ x:"x", y:"y" },
      				label:"Elevation Chart"
      });
      var xA = new dojo.charting.Axis();
      xA.range={upper:#{@gpx.to_hour_min(@gpx.end_time)}, lower:#{@gpx.to_hour_min(@gpx.start_time)}};
    	xA.origin="max";
    	xA.showTicks = true;
    	xA.label = "Time";
    	xA.labels = [#{timeLabel}];
    	
    	var yA = new dojo.charting.Axis();
      yA.range={upper:#{@gpx.max_ele},lower:#{@gpx.min_ele}};
      yA.labels = [ {label:"#{@gpx.min_ele.round}", value:#{@gpx.min_ele} }, { label:"#{@gpx.max_ele.round}",value:#{@gpx.max_ele} }];
      yA.showLines = true;
      yA.showTicks = true;
      yA.label = "Elevation (meters)";
      
      var p = new dojo.charting.Plot(xA, yA);
      p.addSeries({ data:s1, plotter: dojo.charting.Plotters.CurvedLine });
      
      var pA = new dojo.charting.Plot(xA, yA);
      pA.renderType = dojo.charting.RenderPlotSeries.Grouped;

      var pa = new dojo.charting.PlotArea();
      pa.size={width:700,height:300};
      pa.padding={top:20, right:20, bottom:30, left:50 };
      pa.plots.push(p);
      pa.plots.push(pA);

      s1.color = pa.nextColor();

      var chart = new dojo.charting.Chart(null, "Test chart", "This is a potential description");
      chart.addPlotArea({ x:50,y:50, plotArea:pa });
      dojo.addOnLoad(function() {
        chart.node = dojo.byId("chartTest1");
      	chart.render();
      });
</script>
<style>
    			#chartTest1 {
    				margin:12px;
    				width:800px;
    				height:400px;
    				background-color:#dedeed;
    				border:1px solid #999;
    			}
</style> 
EOS
  end
  
end

# test the googlemap generation

track = "21-oct-2007"
gmap = ElevationChart.new(track + ".gpx")
file = File.new(track + "-elevation.html", "w")
file.puts gmap.html()
#puts gmap.elevation_time[0..gmap.elevation_time.length-3]
file.close
